package com.workerhelping.controller;

import com.workerhelping.po.IncomeCategory;
import com.workerhelping.result.Result;
import com.workerhelping.service.IncomeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/incomeCategory")
public class IncomeCategoryController {

    @Autowired
    private IncomeCategoryService incomeCategoryService;

    /**
     * 获取所有收入类别
     *
     * @return
     */
    @GetMapping("/getIncomeCategory")
    public Result<List<IncomeCategory>> getIncomeCategory() {
        List<IncomeCategory> categories = incomeCategoryService.list();
        return Result.ok(categories);
    }

    @PostMapping("/{id}")
    public Result<IncomeCategory> getCategoryById(@PathVariable("id") Integer id) {
        try {
            IncomeCategory incomeCategory = incomeCategoryService.getById(id);
            return Result.ok(incomeCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
